package com.rongma.waterdrop.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.rongma.waterdrop.receiver.TimerChangeReceiver;

import java.util.Timer;
import java.util.TimerTask;

public class TimerService extends Service {

    private Timer timer;
    private int time = 0;
    public TimerService()
    {
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                time++;
                Intent intent = new Intent(TimerChangeReceiver.TIMER_RECEIVER_ACTION);
                intent.putExtra("time",time);
                sendBroadcast(intent);
            }
        },1000,1000);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(timer != null)
        {
            timer.cancel();
            timer = null;
        }
    }

}
